summaryrefslogtreecommitdiff
path: root/src/pages/[locale]/news/[...slug].astro
blob: e3b8b3676ccc00017cca6c916ec57bef789dfb1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
---
import locales from '$i18n/locales';
import Article from '$layouts/Article.astro';
import type Locale from '$types/Locale';
import { CollectionEntry, getCollection } from 'astro:content';

export async function getStaticPaths() {
  function localeFromSlug(slug: CollectionEntry<'news'>['slug']): Locale | undefined {
    const lowerCaseLocale = new RegExp(`^(${locales.map((l) => l.toLocaleLowerCase()).join('|')})`);
    const matchedLowerCaseLocale = slug.match(lowerCaseLocale)?.[1];
    switch (matchedLowerCaseLocale) {
      case 'sco':
        return 'sco';
      case 'en-gb':
        return 'en-GB';
      default:
        console.warn('Could not find locale in slug %s', slug);
        return undefined;
    }
  }

  return (await getCollection('news')).map((entry) => ({
    params: {
      slug: entry.slug.replace('/', '-'),
      locale: localeFromSlug(entry.slug),
    },
    props: { entry },
  }));
}

const { entry } = Astro.props;
const { Content } = await entry.render();
---

<Article title={entry.data.title}>
  <section>
    <h1>{entry.data.title}</h1>
    <Content />
  </section>
</Article>